﻿// TagManagerForm.cs
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Windows.Forms;

public partial class TagManagerForm : Form
{

    // 构造函数
    public TagManagerForm()
    {
        InitializeComponent();
        LoadTags();
    }

    private void LoadTags()
    {
        lstTags.Items.Clear();
        using (var conn = new SQLiteConnection("Data Source=bookmarks.db;Version=3;"))
        {
            conn.Open();

            // 创建临时标签表（如果不存在）
            string createTableSql = @"CREATE TABLE IF NOT EXISTS Tags (
                                    ID INTEGER PRIMARY KEY AUTOINCREMENT,
                                    Name TEXT NOT NULL UNIQUE
                                 );";
            new SQLiteCommand(createTableSql, conn).ExecuteNonQuery();

            string sql = "SELECT Name FROM Tags ORDER BY Name";
            using (var cmd = new SQLiteCommand(sql, conn))
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    lstTags.Items.Add(reader["Name"].ToString());
                }
            }
        }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        string newTag = txtNewTag.Text.Trim();
        if (!string.IsNullOrEmpty(newTag))
        {
            using (var conn = new SQLiteConnection("Data Source=bookmarks.db;Version=3;"))
            {
                conn.Open();
                string sql = "INSERT INTO Tags (Name) VALUES (@name)";
                var cmd = new SQLiteCommand(sql, conn);
                cmd.Parameters.AddWithValue("@name", newTag);

                try
                {
                    cmd.ExecuteNonQuery();
                    LoadTags(); // 刷新列表
                    txtNewTag.Clear();
                }
                catch (SQLiteException ex) when (ex.ErrorCode == (int)SQLiteErrorCode.Constraint_Unique)
                {
                    MessageBox.Show("标签名称已存在！");
                }
            }
        }
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        if (lstTags.SelectedIndex != -1)
        {
            string selectedTag = lstTags.SelectedItem.ToString();
            using (var conn = new SQLiteConnection("Data Source=bookmarks.db;Version=3;"))
            {
                conn.Open();
                string sql = "DELETE FROM Tags WHERE Name = @name";
                var cmd = new SQLiteCommand(sql, conn);
                cmd.Parameters.AddWithValue("@name", selectedTag);
                cmd.ExecuteNonQuery();
                LoadTags(); // 刷新列表
            }
        }
    }


    private List<string> _tags = new List<string>();

    private ListBox lstTags;
    private TextBox txtNewTag;
    private Button btnAdd;
    private Button btnDelete;
    private void InitializeComponent()
    {
        this.lstTags = new System.Windows.Forms.ListBox();
        this.txtNewTag = new System.Windows.Forms.TextBox();
        this.btnAdd = new System.Windows.Forms.Button();
        this.btnDelete = new System.Windows.Forms.Button();
        this.SuspendLayout();

        // lstTags
        this.lstTags.FormattingEnabled = true;
        this.lstTags.ItemHeight = 17;
        this.lstTags.Location = new System.Drawing.Point(12, 12);
        this.lstTags.Name = "lstTags";
        this.lstTags.Size = new System.Drawing.Size(200, 208);
        this.lstTags.TabIndex = 0;

        // txtNewTag
        this.txtNewTag.Location = new System.Drawing.Point(12, 226);
        this.txtNewTag.Name = "txtNewTag";
        this.txtNewTag.Size = new System.Drawing.Size(120, 23);
        this.txtNewTag.TabIndex = 1;

        // btnAdd
        this.btnAdd.Location = new System.Drawing.Point(138, 226);
        this.btnAdd.Name = "btnAdd";
        this.btnAdd.Size = new System.Drawing.Size(75, 25);
        this.btnAdd.TabIndex = 2;
        this.btnAdd.Text = "添加";
        this.btnAdd.UseVisualStyleBackColor = true;
        this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);

        // btnDelete
        this.btnDelete.Location = new System.Drawing.Point(12, 255);
        this.btnDelete.Name = "btnDelete";
        this.btnDelete.Size = new System.Drawing.Size(75, 25);
        this.btnDelete.TabIndex = 3;
        this.btnDelete.Text = "删除";
        this.btnDelete.UseVisualStyleBackColor = true;
        this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);

        // TagManagerForm
        this.ClientSize = new System.Drawing.Size(224, 291);
        this.Controls.Add(this.btnDelete);
        this.Controls.Add(this.btnAdd);
        this.Controls.Add(this.txtNewTag);
        this.Controls.Add(this.lstTags);
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "TagManagerForm";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
        this.Text = "标签管理";
        this.ResumeLayout(false);
        this.PerformLayout();
    }

    // 事件处理方法...
}